﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using HyMaterialStatistic;

namespace HyCommonTableTool
{
	public class GridExtendedData
	{
		public static void WriteGridExtendedData(XYZ ptPosition, ref Group groupGrid, List<short> GridRows, List<short> GridCols, List<CellExtendedData> cellExtendedInfo, GridDataRefresh dataRefresh)
		{
			if (groupGrid == null || cellExtendedInfo.Count <= 0)
			{
				return;
			}
			Guid guid = new Guid("4F8061E6-11F5-4FA4-B5F6-B1913E18A434");
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName("GridExtendedData");
				schemaBuilder.AddSimpleField("Position", typeof(XYZ)).SetUnitType(0);
				schemaBuilder.AddArrayField("GridRows", typeof(short));
				schemaBuilder.AddArrayField("GridCols", typeof(short));
				schemaBuilder.AddArrayField("CellFirstRow", typeof(int));
				schemaBuilder.AddArrayField("CellLastRow", typeof(int));
				schemaBuilder.AddArrayField("CellFirstCol", typeof(int));
				schemaBuilder.AddArrayField("CellLastCol", typeof(int));
				schemaBuilder.AddArrayField("CellElement", typeof(int));
				schemaBuilder.AddArrayField("CellBorder", typeof(string));
				schemaBuilder.AddSimpleField("DocumentName", typeof(string));
				schemaBuilder.AddArrayField("ViewPlan", typeof(int));
				schemaBuilder.AddSimpleField("SelViewPlan", typeof(int));
				schemaBuilder.AddArrayField("FloorName", typeof(string));
				schemaBuilder.AddArrayField("StandFloors", typeof(int));
				schemaBuilder.AddArrayField("FloorCount", typeof(int));
				schemaBuilder.AddArrayField("StandardFloor", typeof(int));
				schemaBuilder.AddArrayField("SelCurveName", typeof(string));
				schemaBuilder.AddArrayField("SelPoints", typeof(XYZ)).SetUnitType(0);
				schemaBuilder.AddSimpleField("StatisticType", typeof(int));
				schemaBuilder.AddSimpleField("LinkFile", typeof(bool));
				schemaBuilder.AddSimpleField("GridTemplate", typeof(string));
				schemaBuilder.AddSimpleField("GridName", typeof(string));
				schemaBuilder.AddArrayField("ColumnWidth", typeof(short));
				schemaBuilder.AddSimpleField("FontSize", typeof(float)).SetUnitType(0);
				schemaBuilder.AddSimpleField("TitleSize", typeof(float)).SetUnitType(0);
				schemaBuilder.AddSimpleField("FontName", typeof(string));
				schemaBuilder.AddSimpleField("LineWidthOut", typeof(int));
				schemaBuilder.AddSimpleField("LineWidthInner", typeof(int));
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField("Position");
			entity.Set<XYZ>(field, ptPosition, (Autodesk.Revit.DB.DisplayUnitType)6);
			Field field2 = schema.GetField("GridRows");
			IList<short> list = new List<short>();
			foreach (short item in GridRows)
			{
				list.Add(item);
			}
			entity.Set<IList<short>>(field2, list);
			Field field3 = schema.GetField("GridCols");
			IList<short> list2 = new List<short>();
			foreach (short item2 in GridCols)
			{
				list2.Add(item2);
			}
			entity.Set<IList<short>>(field3, list2);
			Field field4 = schema.GetField("CellFirstRow");
			Field field5 = schema.GetField("CellFirstCol");
			Field field6 = schema.GetField("CellLastRow");
			Field field7 = schema.GetField("CellLastCol");
			Field field8 = schema.GetField("CellElement");
			Field field9 = schema.GetField("CellBorder");
			IList<int> list3 = new List<int>();
			IList<int> list4 = new List<int>();
			IList<int> list5 = new List<int>();
			IList<int> list6 = new List<int>();
			IList<int> list7 = new List<int>();
			IList<string> list8 = new List<string>();
			foreach (CellExtendedData cellExtendedData in cellExtendedInfo)
			{
				list3.Add(cellExtendedData.FirstRow);
				list4.Add(cellExtendedData.FirstCol);
				list5.Add(cellExtendedData.LastRow);
				list6.Add(cellExtendedData.LastCol);
				if (cellExtendedData.Id == null)
				{
					list7.Add(-1);
				}
				else
				{
					list7.Add(cellExtendedData.Id.IntegerValue);
				}
				list8.Add(cellExtendedData.BorderStyle);
			}
			entity.Set<IList<int>>(field4, list3);
			entity.Set<IList<int>>(field6, list5);
			entity.Set<IList<int>>(field5, list4);
			entity.Set<IList<int>>(field7, list6);
			entity.Set<IList<int>>(field8, list7);
			entity.Set<IList<string>>(field9, list8);
			Field field10 = schema.GetField("DocumentName");
			entity.Set<string>(field10, dataRefresh.DocName);
			IList<int> list9 = new List<int>();
			foreach (ElementId elementId in dataRefresh.ViewPlans)
			{
				list9.Add(elementId.IntegerValue);
			}
			Field field11 = schema.GetField("ViewPlan");
			entity.Set<IList<int>>(field11, list9);
			Field field12 = schema.GetField("SelViewPlan");
			if (dataRefresh.SelCurViewPlan == null)
			{
				entity.Set<int>(field12, -1);
			}
			else
			{
				entity.Set<int>(field12, dataRefresh.SelCurViewPlan.IntegerValue);
			}
			IList<string> list10 = new List<string>();
			IList<int> list11 = new List<int>();
			IList<int> list12 = new List<int>();
			IList<int> list13 = new List<int>();
			foreach (StandardLevel standardLevel in dataRefresh.StandardFloor)
			{
				foreach (ElementId elementId2 in standardLevel.LevelsId)
				{
					list10.Add(standardLevel.LevelName);
					list11.Add(elementId2.IntegerValue);
					list12.Add(standardLevel.Count);
					list13.Add(standardLevel.StandardId.IntegerValue);
				}
			}
			Field field13 = schema.GetField("FloorName");
			entity.Set<IList<string>>(field13, list10);
			Field field14 = schema.GetField("StandFloors");
			entity.Set<IList<int>>(field14, list11);
			Field field15 = schema.GetField("FloorCount");
			entity.Set<IList<int>>(field15, list12);
			Field field16 = schema.GetField("StandardFloor");
			entity.Set<IList<int>>(field16, list13);
			IList<string> list14 = new List<string>();
			IList<XYZ> list15 = new List<XYZ>();
			foreach (SelectAreaCurve selectAreaCurve in dataRefresh.SelAreaCurve)
			{
				foreach (XYZ item3 in selectAreaCurve.Points)
				{
					list14.Add(selectAreaCurve.CurveName);
					list15.Add(item3);
				}
			}
			Field field17 = schema.GetField("SelCurveName");
			entity.Set<IList<string>>(field17, list14);
			Field field18 = schema.GetField("SelPoints");
            entity.Set<IList<XYZ>>(field18, list15, (DisplayUnitType)6);
			Field field19 = schema.GetField("StatisticType");
			entity.Set<int>(field19, dataRefresh.StatisticType);
			Field field20 = schema.GetField("LinkFile");
			entity.Set<bool>(field20, dataRefresh.GridSetInfo.LinkFile);
			Field field21 = schema.GetField("GridTemplate");
			entity.Set<string>(field21, dataRefresh.GridSetInfo.TemplateName);
			Field field22 = schema.GetField("GridName");
			entity.Set<string>(field22, dataRefresh.GridSetInfo.GridName);
			Field field23 = schema.GetField("ColumnWidth");
			entity.Set<IList<short>>(field23, dataRefresh.GridSetInfo.LstColumnWidth);
			Field field24 = schema.GetField("FontSize");
            entity.Set<float>(field24, dataRefresh.GridSetInfo.FontSize, (DisplayUnitType)6);
			Field field25 = schema.GetField("TitleSize");
            entity.Set<float>(field25, dataRefresh.GridSetInfo.TitleFontSize, (DisplayUnitType)6);
			Field field26 = schema.GetField("FontName");
			entity.Set<string>(field26, dataRefresh.GridSetInfo.FontName);
			Field field27 = schema.GetField("LineWidthOut");
			entity.Set<int>(field27, dataRefresh.GridSetInfo.LineWidthOut);
			Field field28 = schema.GetField("LineWidthInner");
			entity.Set<int>(field28, dataRefresh.GridSetInfo.LineWidthInner);
			groupGrid.SetEntity(entity);
		}

		public static bool ReadGridExtendedData(ref XYZ ptPosition, Group groupGrid, ref List<short> GridRows, ref List<short> GridCols, ref List<CellExtendedData> cellExtendedInfo, ref GridDataRefresh dataRefresh)
		{
			if (groupGrid == null)
			{
				return false;
			}
			Schema schema = Schema.Lookup(new Guid("4F8061E6-11F5-4FA4-B5F6-B1913E18A434"));
			if (schema == null)
			{
				return false;
			}
			Entity entity = groupGrid.GetEntity(schema);
			if (entity == null)
			{
				return false;
			}
			if (entity.Schema == null)
			{
				return false;
			}
			Field field = schema.GetField("Position");
			ptPosition = entity.Get<XYZ>(field, (Autodesk.Revit.DB.DisplayUnitType)6);
			Field field2 = schema.GetField("GridRows");
			foreach (short item in entity.Get<IList<short>>(field2))
			{
				GridRows.Add(item);
			}
			Field field3 = schema.GetField("GridCols");
			foreach (short item2 in entity.Get<IList<short>>(field3))
			{
				GridCols.Add(item2);
			}
			Field field4 = schema.GetField("CellFirstRow");
			IList<int> list = entity.Get<IList<int>>(field4);
			Field field5 = schema.GetField("CellLastRow");
			IList<int> list2 = entity.Get<IList<int>>(field5);
			Field field6 = schema.GetField("CellFirstCol");
			IList<int> list3 = entity.Get<IList<int>>(field6);
			Field field7 = schema.GetField("CellLastCol");
			IList<int> list4 = entity.Get<IList<int>>(field7);
			Field field8 = schema.GetField("CellElement");
			IList<int> list5 = entity.Get<IList<int>>(field8);
			Field field9 = schema.GetField("CellBorder");
			IList<string> list6 = entity.Get<IList<string>>(field9);
			for (int i = 0; i < list.Count; i++)
			{
				int firstRow = list[i];
				int firstCol = list3[i];
				int lastRow = list2[i];
				int lastCol = list4[i];
				int num = list5[i];
				ElementId id = null;
				if (num != -1)
				{
					id = new ElementId(num);
				}
				string borderStyle = list6[i];
				cellExtendedInfo.Add(new CellExtendedData(firstRow, firstCol, lastRow, lastCol, id, borderStyle));
			}
			Field field10 = schema.GetField("DocumentName");
			dataRefresh.DocName = entity.Get<string>(field10);
			Field field11 = schema.GetField("ViewPlan");
			foreach (int num2 in entity.Get<IList<int>>(field11))
			{
				dataRefresh.ViewPlans.Add(new ElementId(num2));
			}
			Field field12 = schema.GetField("SelViewPlan");
			int num3 = entity.Get<int>(field12);
			if (num3 == -1)
			{
				dataRefresh.SelCurViewPlan = null;
			}
			else
			{
				dataRefresh.SelCurViewPlan = new ElementId(num3);
			}
			Field field13 = schema.GetField("FloorName");
			IList<string> list7 = entity.Get<IList<string>>(field13);
			Field field14 = schema.GetField("StandFloors");
			IList<int> list8 = entity.Get<IList<int>>(field14);
			Field field15 = schema.GetField("FloorCount");
			IList<int> list9 = entity.Get<IList<int>>(field15);
			Field field16 = schema.GetField("StandardFloor");
			IList<int> list10 = entity.Get<IList<int>>(field16);
			if (list7.Count > 0)
			{
				string text = list7[0];
				List<ElementId> list11 = new List<ElementId>();
				list11.Add(new ElementId(list8[0]));
				if (list7.Count == 1)
				{
					StandardLevel standardLevel = new StandardLevel();
					standardLevel.LevelName = text;
					standardLevel.LevelsId = list11;
					standardLevel.Count = list9[0];
					standardLevel.StandardId = new ElementId(list10[0]);
					dataRefresh.StandardFloor.Add(standardLevel);
				}
				for (int j = 1; j < list7.Count; j++)
				{
					if (text == list7[j])
					{
						list11.Add(new ElementId(list8[j]));
					}
					else
					{
						StandardLevel standardLevel2 = new StandardLevel();
						standardLevel2.LevelName = text;
						standardLevel2.LevelsId = list11;
						standardLevel2.Count = list9[j - 1];
						standardLevel2.StandardId = new ElementId(list10[j - 1]);
						dataRefresh.StandardFloor.Add(standardLevel2);
						text = list7[j];
						list11 = new List<ElementId>();
						list11.Add(new ElementId(list8[j]));
					}
					if (j == list7.Count - 1)
					{
						StandardLevel standardLevel3 = new StandardLevel();
						standardLevel3.LevelName = text;
						standardLevel3.LevelsId = list11;
						standardLevel3.Count = list9[j];
						standardLevel3.StandardId = new ElementId(list10[j]);
						dataRefresh.StandardFloor.Add(standardLevel3);
						break;
					}
				}
			}
			Field field17 = schema.GetField("SelCurveName");
			IList<string> list12 = entity.Get<IList<string>>(field17);
			Field field18 = schema.GetField("SelPoints");
            IList<XYZ> list13 = entity.Get<IList<XYZ>>(field18, (DisplayUnitType)6);
			if (list12.Count > 0)
			{
				string text2 = list12[0];
				List<XYZ> list14 = new List<XYZ>();
				list14.Add(list13[0]);
				if (list12.Count == 1)
				{
					SelectAreaCurve selectAreaCurve = new SelectAreaCurve();
					selectAreaCurve.CurveName = text2;
					selectAreaCurve.Points = list14;
					dataRefresh.SelAreaCurve.Add(selectAreaCurve);
				}
				for (int k = 1; k < list12.Count; k++)
				{
					if (text2 == list12[k])
					{
						list14.Add(list13[k]);
					}
					else
					{
						SelectAreaCurve selectAreaCurve2 = new SelectAreaCurve();
						selectAreaCurve2.CurveName = text2;
						selectAreaCurve2.Points = list14;
						dataRefresh.SelAreaCurve.Add(selectAreaCurve2);
						text2 = list12[k];
						list14 = new List<XYZ>();
						list14.Add(list13[k]);
					}
					if (k == list12.Count - 1)
					{
						SelectAreaCurve selectAreaCurve3 = new SelectAreaCurve();
						selectAreaCurve3.CurveName = text2;
						selectAreaCurve3.Points = list14;
						dataRefresh.SelAreaCurve.Add(selectAreaCurve3);
						break;
					}
				}
			}
			Field field19 = schema.GetField("StatisticType");
			dataRefresh.StatisticType = entity.Get<int>(field19);
			Field field20 = schema.GetField("LinkFile");
			dataRefresh.GridSetInfo.LinkFile = entity.Get<bool>(field20);
			Field field21 = schema.GetField("GridTemplate");
			dataRefresh.GridSetInfo.TemplateName = entity.Get<string>(field21);
			Field field22 = schema.GetField("GridName");
			dataRefresh.GridSetInfo.GridName = entity.Get<string>(field22);
			Field field23 = schema.GetField("ColumnWidth");
			dataRefresh.GridSetInfo.LstColumnWidth = entity.Get<IList<short>>(field23).ToList<short>();
			Field field24 = schema.GetField("FontSize");
            dataRefresh.GridSetInfo.FontSize = entity.Get<float>(field24, (DisplayUnitType)6);
			Field field25 = schema.GetField("TitleSize");
            dataRefresh.GridSetInfo.TitleFontSize = entity.Get<float>(field25, (DisplayUnitType)6);
			Field field26 = schema.GetField("FontName");
			dataRefresh.GridSetInfo.FontName = entity.Get<string>(field26);
			Field field27 = schema.GetField("LineWidthOut");
			dataRefresh.GridSetInfo.LineWidthOut = entity.Get<int>(field27);
			Field field28 = schema.GetField("LineWidthInner");
			dataRefresh.GridSetInfo.LineWidthInner = entity.Get<int>(field28);
			return true;
		}

		public static bool GroupHasExtendedData(Group group)
		{
			if (group == null)
			{
				return false;
			}
			Schema schema = Schema.Lookup(new Guid("4F8061E6-11F5-4FA4-B5F6-B1913E18A434"));
			if (schema == null)
			{
				return false;
			}
			Entity entity = group.GetEntity(schema);
			return entity != null && entity.Schema != null;
		}

		private const string GRID_GUID = "4F8061E6-11F5-4FA4-B5F6-B1913E18A434";

		private const string m_SchemaName = "GridExtendedData";

		private const string m_GridRows = "GridRows";

		private const string m_GridCols = "GridCols";

		private const string m_Position = "Position";

		private const string m_FirstRow = "CellFirstRow";

		private const string m_LastRow = "CellLastRow";

		private const string m_FirstCol = "CellFirstCol";

		private const string m_LastCol = "CellLastCol";

		private const string m_ElementId = "CellElement";

		private const string m_CellBorder = "CellBorder";

		private const string m_DocName = "DocumentName";

		private const string m_Floor = "ViewPlan";

		private const string m_SelViewPlan = "SelViewPlan";

		private const string m_FloorName = "FloorName";

		private const string m_StanFloors = "StandFloors";

		private const string m_FloorCount = "FloorCount";

		private const string m_Standard = "StandardFloor";

		private const string m_SelCurveName = "SelCurveName";

		private const string m_SelCurvePt = "SelPoints";

		private const string m_Statistic = "StatisticType";

		private const string m_LinkFile = "LinkFile";

		private const string m_Template = "GridTemplate";

		private const string m_GridName = "GridName";

		private const string m_ColumnWidth = "ColumnWidth";

		private const string m_FontSize = "FontSize";

		private const string m_TitleSize = "TitleSize";

		private const string m_FontName = "FontName";

		private const string m_OutWidth = "LineWidthOut";

		private const string m_InnerWidth = "LineWidthInner";
	}
}
